package com.retlife.tvshow.serial_port.bike

import com.retlife.tvshow.serial_port.bike.model.BikeData

class DecodeData {


    companion object {
        fun decodeData(bytes: ByteArray): BikeData {
            val bikeData = BikeData()
            bikeData.speed = (bytes[13].toInt() shl 8) or bytes[14].toInt()
            bikeData.torqueAverage = (bytes[15].toInt() shl 8) or bytes[16].toInt()

            return bikeData
        }



        /*private fun getBytesByCharArray(chars: CharArray): ByteArray {
            val cs: Charset = StandardCharsets.UTF_8
            val cb: CharBuffer = CharBuffer.allocate(chars.size)
            cb.put(chars)
            cb.flip()
            val bb: ByteBuffer = cs.encode(cb)
            return bb.array()
        }*/
        const val NUT_RESET_ID = 0x0101
        const val NUT_BIKE_TORQUE_DATA_CODE = 0xE00E
        const val NUT_BIKE_SYSTEM_SETTING_CODE = 0xE11E
        const val NUT_BIKE_TORQUE_STATUS_CODE = 0x6886
        const val NUT_POWER_STATUS_CODE = 0x6776
        const val NUT_POWER1_SET_LOAD_ID = 0xFCCF
        const val NUT_POWER2_SET_LOAD_ID = 0xFCCE
        const val NUT_POWER_SWITCH_ID = 0xFCCD
        const val NUT_CYCLES_CLEAR_ID = 0xFBB3


        const val NUT_START_ID = 0xFAA0 // START

        const val NUT_PAUSE_ID = 0xFAA1 // PAUSE

        const val NUT_STOP_ID = 0xFAA2 // PAUSE

        const val NUT_SET_TORQUE_ID = 0xFAA3 // PAUSE

        const val NUT_SET_POWER_ID = 0xFAA4 //

        const val NUT_SET_BUF_SIZE_ID = 0xFAA5 //

        const val NUT_SEND_SAMPLE_TIME_ID = 0xFAA6 //发送时间间隔


        const val NUT_PROGRAM_ID = 0x99FE

        const val NUT_SET_SAMPLE = 0x2220

        const val NUT_TORQUE_200HZ_ID = 0xF220
        const val NUT_TORQUE_100HZ_ID = 0xF100
        const val NUT_TORQUE_10HZ_ID = 0xF010
        const val NUT_TORQUE_RESET_ID = 0xFBB0
        const val NUT_TORQUE_ASK_ID = 0xFBB1
        const val NUT_TORQUE_STOP_ID = 0xFBB2

        // 测试等速命令
        const val NUT_ISO_STATUS_ID = 0x1516
        const val NUT_ISO_SET_SPEED = 0x1517
        const val NUT_ISO_POSITION_START = 0x1518
        const val NUT_ISO_POSITION_STOP = 0x1519
        const val NUT_ISO_TRIGGER_CCW = 0x1520
        const val NUT_ISO_TRIGGER_CW = 0x1521


        // 读取运行程序ID  // A5 5A 06 83 99 FE 00 00 00
        val READ_PROGRAM_ID = byteArrayOf(0XA5.toByte(), 0X5A, 0X06, 0X83.toByte(), 0X99.toByte(), 0XFE.toByte(), 0X00.toByte(), 0X00.toByte(), 0X00.toByte())

        // 系统复位       //A5 5A 06 83 00 01 01 00 00
        val RESET_SYSTEM = byteArrayOf(0XA5.toByte(), 0X5A.toByte(), 0X06.toByte(), 0X83.toByte(), 0X00.toByte(), 0X01, 0X01, 0X00, 0X00)

        // 读传感器状态   //A5 5A 06 99 F2 00 00 00 00
        val READ_SENSOR_STATUS = byteArrayOf(0XA5.toByte(), 0X5A.toByte(), 0X06.toByte(), 0X99.toByte(), 0XF2.toByte(), 0X00.toByte(), 0X00.toByte(), 0X00, 0X00)

    }
}
